import time

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

from common.DriverTools import DriverTools
from common.utils import get_el_text
from page.HomePage import HomePage
from page.LoginPage import LoginPage


class TestLogin(object):
    def setup_class(self):
        # 初始化对象
        self.driver=DriverTools.get_driver()
        self.login_page=LoginPage()

    def teardown_class(self):
        # 关闭驱动对象
        DriverTools.quit_driver()

    def setup(self):
        # 打开测试页面
        self.driver.get("http://hmshop-test.itheima.net/")
        # 进入登录页面
        HomePage().goto_login_page()

    def teardown(self):
        try:
            self.driver.find_element(By.LINK_TEXT,'安全退出').click()
        except Exception:
            print("当前处于未登录状态")

    # 登录成功
    def test_login_success(self):
        # 调用登录方法完成登录
        self.login_page.login("13045645678","123456","8888")
        time.sleep(2)
        # 打印登录成功后的手机号
        userinfo=get_el_text(self.driver,By.CLASS_NAME,"userinfo")
        print(f"登录成功后的手机号为：{userinfo}")
        assert "13012345678"==userinfo

    # 登录失败（账号不存在）
    def test_login_fail_user_not_exist(self):
        # 调用登录方法完成登录
        self.login_page.login("13012345000", "123456", "8888")
        time.sleep(2)
        # 打印登录失败后的提示信息
        # alert = self.driver.find_element(By.CLASS_NAME, 'layui-layer-content')
        alert_text=get_el_text(self.driver,By.CLASS_NAME, 'layui-layer-content')
        print(f"登录失败时的错误提示为：{alert_text}")
        assert "账号不存在" in alert_text

    # 密码错误
    def test_login_fail_pwd_error(self):
        # 调用登录方法完成登录
        self.login_page.login("13012345678", "111111", "8888")
        time.sleep(2)
        # 打印登录失败后的提示信息
        alert_text=get_el_text(self.driver,By.CLASS_NAME, 'layui-layer-content')
        print(f"登录失败时的错误提示为：{alert_text}")
        assert "密码错误" in alert_text